﻿using ShoppingEntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace WebApplication.Controllers
{
    public class HomeController : Controller
    {
        ShoppingEntities db = new ShoppingEntities();

      
        public ActionResult Index()
        {
          

            W_SYS_Users user= new W_SYS_Users();

            user = db.W_SYS_Users.FirstOrDefault();

            
            return View();
        }
        //登录
        public ActionResult Login()
        {
            ViewBag.Message = "Your application";

            return View();
        }

        //登录处理事件
        [HttpPost]
        public ActionResult LoginAction(string phonenumber,string password)
        {

            // 获取表单数据
            string PhoneNumber = phonenumber;
            string PassWord = EncryptFieldWithMD5(password);
            var User = db.W_SYS_Users.FirstOrDefault(r=>r.PhoneNumber==phonenumber && r.PassWord== PassWord);
            if (User!=null)
            {
              
                return Json(new { success = true, message = "登录成功，即将跳转到首页！" });
            }
            else
            {
                return Json(new { success = false, message = "登录失败，该用户不存在，请检查用户名或密码是否有误！" });
            }
            // 处理你的逻辑

            // 返回操作结果
           // return Json(new { success = true, message = "提交成功" });
        }
    
    //注册
    public ActionResult Register()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }


        //注册处理事件
        [HttpPost]
        public ActionResult RegisterAction(string phonenumber, string password,string nickname)
        {

            // 获取表单数据
            string PhoneNumber = phonenumber;
            string PassWord = password;
            string Nickname = nickname;
            var User = db.W_SYS_Users.FirstOrDefault(r => r.PhoneNumber == phonenumber);
            if (User != null)
            {
               return Json(new { success = false, message = "注册失败，该用户已存在，请检查手机号是否有误！" });
             
            }
            else
            {
                PassWord = EncryptFieldWithMD5(PassWord);
                W_SYS_Users user = new W_SYS_Users();
                user.UUID =Guid.NewGuid();
                user.PhoneNumber = PhoneNumber;
                user.PassWord = PassWord;
                user.UserName = nickname;
                db.W_SYS_Users.Add(user);
                db.SaveChanges();
                

                return Json(new { success = true, message = "注册成功，即将跳转到登录页面！" });
            }
            // 处理你的逻辑

            // 返回操作结果
            // return Json(new { success = true, message = "提交成功" });
        }



        #region MD5加密
        public string EncryptFieldWithMD5(string fieldValue)
        {
            // 创建一个MD5对象
            using (MD5 md5Hash = MD5.Create())
            {
                // 将输入字符串转换为字节数组并计算其MD5哈希
                byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(fieldValue));

                // 创建一个新的Stringbuilder来收集字节并创建一个字符串
                StringBuilder sBuilder = new StringBuilder();

                // 循环遍历每个字节的哈希，并格式化为十六进制字符串
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }

                // 返回十六进制字符串
                return sBuilder.ToString();
            }
        }
        #endregion



    }
}